----------------------------------Spell Variables--------------------------------------
SPELL.m_iSpellID = 12102 -- Whatever value you want as long as it doesn't override any other spells' IDs.
SPELL.m_iRange = 1024
SPELL.m_sName = "Blood Funnel"
SPELL.m_sToolTip =	[[Drain the target's blood,
					stealing 5 HP per second for
					5 seconds.]]
SPELL.m_sIcon = "devin/blackbetty/spells/lifebinder/bloodfunnel.jpg"
SPELL.m_iCoolDown = 5
SPELL.m_iManaCost = 50
SPELL.m_bRequiresTarget = true
SPELL.m_iSpellType = SPELLTYPE_RANGEDAMAGE

---------------------------------------------------------------------------------------

function SPELL:Cast( pl, target )

	local range = self.m_iRange
	
	print("Casted "..self.m_sName.."!")
	if(target && target:IsValid() && target != pl) then
		self.CastSound = CreateSound( pl, "devin/blackbetty/weapons/castingloops/Shadow.wav" )
		self.CastSound:Play()
		ParticleEffectAttach( "blood_hands", PATTACH_POINT_FOLLOW, pl, pl:LookupAttachment("anim_attachment_RH"))
		local rope = ents.Create( "keyframe_rope" )
		rope:SetPos( pl:GetPos() )
		rope:SetKeyValue( "RopeMaterial", "cable/blood_beam1" )
		rope:SetEntity( "StartEntity", pl)
		rope:SetKeyValue( "StartBone", 10)
		rope:SetKeyValue( "Slack", 64)
		rope:SetKeyValue( "Width", 16)
		rope:SetKeyValue( "StartOffset", tostring(Vector(0, 0, 48)) )
		rope:SetEntity( "EndEntity", pl)
		rope:SetKeyValue( "EndOffset", tostring(Vector(0, 0, 48)) )
		rope:Spawn()
        rope:Activate()
		rope:SetEntity( "EndEntity", target)
		timer.Simple(5, function()
			pl:SetNWBool("Casting", false)
			pl:SetNWBool("Casted", false)
			pl.DoingCast = false
			self.CastSound:Stop()
			pl:StopParticles()
			rope:Remove()
		end)
		net.Start( "SendPlayerCast" )
		local SendTable = {5, self.m_sName, Color(115, 15, 15, 155)}
		net.WriteTable(SendTable)
		net.Send(pl)
		pl.DoingCast = true
		pl.CastWhileMoving = true
		timer.Create(""..pl:EntIndex().."bloodfunnel", 1, 5, function()
			target:EmitSound("devin/blackbetty/weapons/RendTarget.wav", 80, 100+math.random(-10,10))
			if(pl:IsPlayer())then
				pl:DoAnimationEvent(ACT_HL2MP_GESTURE_RANGE_ATTACK_CROSSBOW)
			end
			pl:EmitSound("devin/blackbetty/weapons/castsounds/ShadowCast.wav", 100, 100+math.random(-10,10))
			if(pl:IsPlayer())then
				local dmginfo = DamageInfo()
					dmginfo:SetDamage( 15 ) 
					dmginfo:SetDamageType( DMG_SLASH ) 
					dmginfo:SetAttacker( pl )
				target:TakeDamageInfo( dmginfo )
				local dmginfo = DamageInfo()
					dmginfo:SetDamage( -10 ) 
					dmginfo:SetDamageType( DMG_POISON ) 
					dmginfo:SetAttacker( pl )
				pl:TakeDamageInfo( dmginfo )
			else
				local dmginfo = DamageInfo()
					dmginfo:SetDamage( 5 ) 
					dmginfo:SetDamageType( DMG_SLASH ) 
					dmginfo:SetAttacker( pl )
				target:TakeDamageInfo( dmginfo )
				local dmginfo = DamageInfo()
					dmginfo:SetDamage( -5 ) 
					dmginfo:SetDamageType( DMG_POISON ) 
					dmginfo:SetAttacker( pl )
				pl:TakeDamageInfo( dmginfo )
			end
				ParticleEffect( "player_blood", target:GetPos() + Vector(0, 0, 45), Angle(0,0,0), target )	
			end)
		else
			pl:SetNWBool("Casting", false)
			pl:SetNWBool("Casted", false)
			pl.DoingCast = false
			pl:StopParticles()
			self.CastSound:Stop()
			return true
	end
end




